package com.site.servlet;

import com.alibaba.fastjson.JSON;
import com.site.pojo.ReceiveDetail;
import com.site.service.ReceiveDetailService;
import com.site.service.impl.ReceiveDetailServiceImpl;
import com.site.utils.CommonResult;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/receiveDetail.do")
public class ReceiveDetailServlet extends HttpServlet {
    private ReceiveDetailService receiveDetailService = new ReceiveDetailServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = null;
        String opr = request.getParameter("opr");
        try{
            out = response.getWriter();
            switch (opr){
                case "getAllDetail":{
                    out.write(JSON.toJSONString(getAllDetail(request,response)));
                    break;
                }
                case "addDetail":{
                    out.write(JSON.toJSONString(addDetail(request,response)));
                    break;
                }
                case "delAppAttr":{
                    out.write(JSON.toJSONString(delAppAttr(request,response)));
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            out.flush();
            out.close();
        }
    }
    public CommonResult delAppAttr(HttpServletRequest request, HttpServletResponse response){
        ServletContext application = request.getServletContext();
        application.removeAttribute("detail");
        return CommonResult.success(null);
    }

    public CommonResult getAllDetail(HttpServletRequest request, HttpServletResponse response){
        CommonResult result = null;
        List<ReceiveDetail> list = receiveDetailService.getAll();
        if(list.size()!=0){
            result = CommonResult.success(list);
        }else {
            result = CommonResult.failed();
        }
        return result;
    }
    public CommonResult addDetail(HttpServletRequest request, HttpServletResponse response)throws Exception{
        CommonResult result = null;
        List<ReceiveDetail> list = new ArrayList<ReceiveDetail>();
        ReceiveDetail receiveDetail = new ReceiveDetail();
        String goodsId = request.getParameter("goodsId");
        Long eid = Long.parseLong("3");
        String rid = request.getParameter("rid");
        String rtid = request.getParameter("radio");
        String receiveCount = request.getParameter("receiveCount");
        receiveDetail.setRid(Long.parseLong(rid));
        receiveDetail.setEid(eid);
        receiveDetail.setGoodsId(Long.parseLong(goodsId));
        receiveDetail.setRtid(Long.parseLong(rtid));
        receiveDetail.setReceiveCount(Long.parseLong(receiveCount));
        receiveDetail.setUnit("根");
        receiveDetail.setOprTime(java.sql.Timestamp.valueOf("2021-12-03 12:12:11"));
        list.add(receiveDetail);
        boolean flag = receiveDetailService.addReceiveDetail(list);
        if(flag){
            result = CommonResult.success(null);
        }else {
            result = CommonResult.failed();
        }
        return result;
    }
}
